-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc][pthread] fix -Wmissing-field-initializers #126314
[libc][pthread] fix -Wmissing-field-initializers #126314
Conversation
Fixes: llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29: warning: missing field '__preference' initializer [-Wmissing-field-initializers] 59 | pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; | ^ Also, add a test that demonstrates the same issue for PTHREAD_MUTEX_INITIALIZER, and fix that, too. PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
@llvm/pr-subscribers-libc Author: Nick Desaulniers (nickdesaulniers) ChangesFixes:
Also, add a test that demonstrates the same issue for PTHREAD_ONCE_INIT does not have this issue and does have test coverage. Full diff: https://github.com/llvm/llvm-project/pull/126314.diff 2 Files Affected:
diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 8a144dbd2e611e7..2b1440f9308ca7f 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -25,8 +25,8 @@
#define PTHREAD_PROCESS_PRIVATE 0
#define PTHREAD_PROCESS_SHARED 1
-#define PTHREAD_MUTEX_INITIALIZER {0}
-#define PTHREAD_RWLOCK_INITIALIZER {0}
+#define PTHREAD_MUTEX_INITIALIZER {}
+#define PTHREAD_RWLOCK_INITIALIZER {}
// glibc extensions
#define PTHREAD_STACK_MIN (1 << 14) // 16KB
diff --git a/libc/test/integration/src/pthread/pthread_mutex_test.cpp b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
index ce2a3538924da84..ec8488da4ead98f 100644
--- a/libc/test/integration/src/pthread/pthread_mutex_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_mutex_test.cpp
@@ -186,6 +186,10 @@ void multiple_waiters() {
LIBC_NAMESPACE::pthread_mutex_destroy(&counter_lock);
}
+// Test the initializer
+[[gnu::unused]]
+static pthread_mutex_t test_initializer = PTHREAD_MUTEX_INITIALIZER;
+
TEST_MAIN() {
relay_counter();
wait_and_step();
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
oh gawd clang-format...my eyes!!! |
Do we really need a macro that uses 10x as many characters as it's replacing. |
Homey, that boat sailed 100 years ago. That feedback should have been to the austin group back when they made up this shit! |
Also, I assume that as opaque types, you could perhaps have non-zero values you'd like to initialize these members to, so declaring They probably also made this up BEFORE they had designated initializers; gotta love aggregate initializers! Don't mess up the order ever! Refactor struct definitions at your peril! |
So, But there may be a format option that fixes this (who knows if it's specific enough though, or too broad). Still digging in https://clang.llvm.org/docs/ClangFormatStyleOptions.html. |
|
This is moot anyway, because you can't use |
@@ -186,6 +186,10 @@ void multiple_waiters() { | |||
LIBC_NAMESPACE::pthread_mutex_destroy(&counter_lock); | |||
} | |||
|
|||
// Test the initializer | |||
[[gnu::unused]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not [[maybe_unused]]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch! I always forget that that one got standardized.
bfc84ca
You're right. https://godbolt.org/z/fnYExjrf5 Aggregate initialization (peasantry) it is, then. God, why have you forsaken me? |
Let me file an issue about our pthread_rwlock_t being linux specific, which has implications for the initializer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the code LGTM, the formatting seems like a pain
/* .__is_pshared = */ 0, \ | ||
/* .__preference = */ 0, \ | ||
/* .__state = */ 0, \ | ||
/* .__write_tid = */ 0, /* .__wait_queue_mutex = */ \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you want these two comments to be on separate lines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes; but dunno how to fix this quickly with clang-format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it work if you make it like:
/* .__wait_queue_mutex = */ {0}, \
/* .__pending_readers = */ {0}, \
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does. That makes me sad to mix comments that look like designated initializers, but only for top level members, with more aggregate initializers without such comments for nested members, but I lack the will to wrestle with the formatter.
I think we could put a .clang-format file in this dir, and use https://clang.llvm.org/docs/ClangFormatStyleOptions.html#whitespacesensitivemacros to turn off formatting for this macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2da1ce2 PTAL
Fixes: llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29: warning: missing field '__preference' initializer [-Wmissing-field-initializers] 59 | pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; | ^ Also, add a test that demonstrates the same issue for PTHREAD_MUTEX_INITIALIZER, and fix that, too. PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
Fixes: llvm-project/libc/test/integration/src/pthread/pthread_rwlock_test.cpp:59:29: warning: missing field '__preference' initializer [-Wmissing-field-initializers] 59 | pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; | ^ Also, add a test that demonstrates the same issue for PTHREAD_MUTEX_INITIALIZER, and fix that, too. PTHREAD_ONCE_INIT does not have this issue and does have test coverage.
Fixes:
Also, add a test that demonstrates the same issue for
PTHREAD_MUTEX_INITIALIZER, and fix that, too.
PTHREAD_ONCE_INIT does not have this issue and does have test coverage.